<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <!-- <a-col :span="24">
            <a-form-model-item label="工单编号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceWorkId">
              <a-input v-model="model.serviceWorkId" placeholder="请输入工单编号"></a-input>
            </a-form-model-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-model-item label="工单内容" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceContent">
              <a-input v-model="model.serviceContent" placeholder="请输入工单内容"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="工单名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workName">
              <a-input v-model="model.workName" placeholder="请输入工单名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workCustomer">
              <a-input v-model="model.workCustomer" placeholder="请输入客户名称"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户手机号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workPhone">
              <a-input v-model="model.workPhone" placeholder="请输入客户手机号"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="客户地址" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workAddress">
              <a-input v-model="model.workAddress" placeholder="请输入客户地址"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="损坏设备" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="serviceEquipment">
              <a-input v-model="model.serviceEquipment" placeholder="请输入损坏设备"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="日期选择框" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="jDate">
              <j-date v-model="model.workStart" :showTime="true" dateFormat="YYYY-MM-DD HH:mm:ss" />
            </a-form-model-item>
          </a-col>

          <!--  字典表下拉 -->
          <a-col :span="24">
            <a-form-model-item label="紧急程度" prop="workUrgent" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag v-model="model.workUrgent" placeholder="请选择紧急程度" dictCode="workUrgent_status" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
import { validateDuplicateValue } from '@/utils/util'

export default {
  name: 'WorkServiceForm',
  components: {},
  props: {
    //表单禁用
    disabled: {
      type: Boolean,
      default: false,
      required: false,
    },
  },
  data() {
    return {
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      validatorRules: {
        serviceWorkId: [{ required: true, message: '请输入工单编号!' }],
        workName: [{ required: true, message: '请输入工单名称!' }],
        workCustomer: [{ required: true, message: '请输入客户名称!' }],
        workPhone: [{ required: true, message: '请输入客户手机号!' }],

        workAddress: [{ required: true, message: '请输入客户地址!' }],
        // jDate: [{ required: true, message: '请选择日期!' }],
        // wrapperCol: [{ required: true, message: '请选择紧急程度!' }],

        serviceContent: [{ required: true, message: '请输入工单内容!' }],
        serviceEquipment: [{ required: true, message: '请输入损坏设备!' }],
      },
      url: {
        add: '/WorkService/workService/add',
        edit: '/WorkService/workService/edit',
        queryById: '/WorkService/workService/queryById',
      },
    }
  },
  computed: {
    formDisabled() {
      return this.disabled
    },
  },
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model))
  },
  methods: {
    add() {
      this.edit(this.modelDefault)
    },
    edit(record) {
      this.model = Object.assign({}, record)
      this.visible = true
    },
    submitForm() {
      const that = this
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let httpurl = ''
          let method = ''
          if (!this.model.id) {
            httpurl += this.url.add
            method = 'post'
          } else {
            httpurl += this.url.edit
            method = 'put'
          }
          httpAction(httpurl, this.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>